﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.IO;
using ActiveStoryTouch.DataModel.Helpers;
using System.Windows.Ink;
using ActiveStoryTouch.DataModel.Wpf;

namespace TestSuite.DataModel
{
    
    
    /// <summary>
    ///This is a test class for PageTest and is intended
    ///to contain all PageTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PageTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Page Constructor
        ///</summary>
        [TestMethod()]
        public void PageConstructorTest()
        {
            Page target = new Page();
            Assert.IsNotNull(target.PrototypeElementDictionary);
            Assert.IsNotNull(target.Strokes);
        }

        [TestMethod]
        public void PageSerializationTest()
        {
            Page target = new Page { Name = CommonTestConstants.TestPageName, PageNumber = 1, UnqiueId = 1 };
            Assert.IsNotNull(target.PrototypeElementDictionary);
            Assert.IsNotNull(target.Strokes);
            Assert.IsNotNull(target.PageCanvasSettings);
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            element.GestureTargetPageMap.Add(CommonTestConstants.TestGestureIdentifier, 1);
            PrototypeElement elementTwo = new PrototypeElement { ElementType = ElementTypes.ListBox, Top = 50.0, Left = 40.0, Width = 20.0, Height = 70.0, Content = "Test two!", ContentVisibile = true, HoverGlow = true, UniqueId = 2 };
            target.PrototypeElementDictionary.Add(element.UniqueId, element);
            target.PrototypeElementDictionary.Add(elementTwo.UniqueId, elementTwo);
            Assert.AreEqual<int>(2, target.PrototypeElementDictionary.Count);

            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection strokeCollection = new StrokeCollection(fs);
            fs.Close();
            SerializableStrokeCollection serializableStrokeCollection = StrokeHelper.ConvertToSerializableStrokeCollection(strokeCollection);
            target.Strokes.AddRange(serializableStrokeCollection);
            Assert.AreNotEqual<int>(0, target.Strokes.Count);
            
            target.SaveToFile(CommonTestConstants.PageSerializationTestFileName);


            DataContractSerializer serializer = new DataContractSerializer(typeof(Page));
            FileStream fileStream = new FileStream(CommonTestConstants.PageSerializationTestFileName, FileMode.Open);
            Page pageFromFile = serializer.ReadObject(fileStream) as Page;
            Assert.IsNotNull(pageFromFile);
            Assert.IsNotNull(pageFromFile.PrototypeElementDictionary);
            Assert.IsNotNull(pageFromFile.Strokes);
            Assert.AreEqual<int>(target.PrototypeElementDictionary.Count, pageFromFile.PrototypeElementDictionary.Count);
            Assert.AreEqual<ElementTypes>(target.PrototypeElementDictionary[1].ElementType, pageFromFile.PrototypeElementDictionary[1].ElementType);
            Assert.AreNotEqual<int>(0, pageFromFile.Strokes.Count);
            Assert.AreEqual<int>(target.Strokes.Count, pageFromFile.Strokes.Count);
            Assert.AreEqual<String>(target.Name, pageFromFile.Name);
            Assert.AreEqual<long>(target.PageNumber, pageFromFile.PageNumber);
            Assert.AreEqual<long>(target.UnqiueId, pageFromFile.UnqiueId);
        }

        [TestMethod]
        public void PageDeserializationTest()
        {
            Page target = new Page { Name = CommonTestConstants.TestPageName, PageNumber = 1, UnqiueId = 1 };
            Assert.IsNotNull(target.PrototypeElementDictionary);
            Assert.IsNotNull(target.Strokes);
            Assert.IsNotNull(target.PageCanvasSettings);
            PrototypeElement element = new PrototypeElement { ElementType = ElementTypesHelper.ElementTypeFromString(CommonTestConstants.TestElementTypeString), Top = 100.0, Left = 200.0, Width = 120.0, Height = 90.0, Content = "Test", ContentVisibile = false, HoverGlow = false, UniqueId = 1 };
            element.GestureTargetPageMap.Add(CommonTestConstants.TestGestureIdentifier, 1);
            PrototypeElement elementTwo = new PrototypeElement { ElementType = ElementTypes.ListBox, Top = 50.0, Left = 40.0, Width = 20.0, Height = 70.0, Content = "Test two!", ContentVisibile = true, HoverGlow = true, UniqueId = 2 };
            target.PrototypeElementDictionary.Add(element.UniqueId, element);
            target.PrototypeElementDictionary.Add(elementTwo.UniqueId, elementTwo);
            Assert.AreEqual<int>(2, target.PrototypeElementDictionary.Count);

            FileStream fs = new FileStream(Path.Combine(CommonTestConstants.RelativeTestProjectDirectory, "StrokeSerializationTest.isf"), FileMode.Open, FileAccess.Read, FileShare.Read);
            StrokeCollection strokeCollection = new StrokeCollection(fs);
            fs.Close();
            SerializableStrokeCollection serializableStrokeCollection = StrokeHelper.ConvertToSerializableStrokeCollection(strokeCollection);
            target.Strokes.AddRange(serializableStrokeCollection);
            Assert.AreNotEqual<int>(0, target.Strokes.Count);

            target.SaveToFile(CommonTestConstants.PageDeserializationTestFileName);

            Page pageFromFile = Page.LoadFromFile(CommonTestConstants.PageDeserializationTestFileName);
            Assert.IsNotNull(pageFromFile);
            Assert.IsNotNull(pageFromFile.PrototypeElementDictionary);
            Assert.IsNotNull(pageFromFile.Strokes);
            Assert.AreEqual<int>(target.PrototypeElementDictionary.Count, pageFromFile.PrototypeElementDictionary.Count);
            Assert.AreEqual<ElementTypes>(target.PrototypeElementDictionary[1].ElementType, pageFromFile.PrototypeElementDictionary[1].ElementType);
            Assert.AreNotEqual<int>(0, pageFromFile.Strokes.Count);
            Assert.AreEqual<int>(target.Strokes.Count, pageFromFile.Strokes.Count);
            Assert.AreEqual<String>(target.Name, pageFromFile.Name);
            Assert.AreEqual<long>(target.PageNumber, pageFromFile.PageNumber);
            Assert.AreEqual<long>(target.UnqiueId, pageFromFile.UnqiueId);
            Assert.AreEqual<int>(target.PrototypeElementDictionary[1].GestureTargetPageMap.Count, pageFromFile.PrototypeElementDictionary[1].GestureTargetPageMap.Count);
            Assert.IsTrue(pageFromFile.PrototypeElementDictionary[1].GestureTargetPageMap.ContainsKey(CommonTestConstants.TestGestureIdentifier));
            Assert.AreEqual<long>(1, pageFromFile.PrototypeElementDictionary[1].GestureTargetPageMap[CommonTestConstants.TestGestureIdentifier]);
        }
    }
}
